package com.yonyou.hxjy.service.dingtalk.Impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.yonyou.hxjy.enums.OpenApiUrlEnum;
import com.yonyou.hxjy.service.BaseApi;
import com.yonyou.hxjy.utils.YsQueryDtoSingletonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class YsDataGetService extends BaseApi {

    private static final Logger logger = LoggerFactory.getLogger(YsDataGetService.class);


    //组织单元编码查询
    public String getOrgUitCode(String name){
        Map<String,String> params = new HashMap<String,String>();
        params.put("name",name);
        params.put("funcTypeCode","orgunit");
        params.put("enable","1");
        String code = null;
        JSONObject jsonObject = null;
        try {
            jsonObject = JSONUtil.parseObj(doPost(YsQueryDtoSingletonUtil.getYsQueryDto().getGatewayUrl() + OpenApiUrlEnum.ORG_QUERY, params, YsQueryDtoSingletonUtil.getYsQueryDto()));

            JSONObject data = jsonObject.getJSONObject("data");

            if (data != null && !data.isEmpty()) {
                JSONArray recordList = data.getJSONArray("recordList");
                for(int i=0;i<recordList.size();i++){
                 code = recordList.getJSONObject(i).getStr("code");
                }
                return code;
            }else {
                throw new Exception("ys系统中没有对应的组织单元");
            }
        } catch (Exception e) {
            //logger.error("错误处理{}", e.getMessage());
        }
        return code;
    }

    //企业银行账户信息获取
    public List<JSONObject> getBankDetailsMessage(){
        //默认银行账户判断
        long pageSize = 500;
        long pageIndex = 1;
        Map<String,Object> paramMap = new HashMap<String,Object>(){
            {
                put("pageSize",pageSize);
                put("pageIndex",1);
            }
        };
        JSONObject jsonObject;
        JSONArray jsonArray;
        List<JSONObject> bankList = new ArrayList<>(); //汇总银行信息
        while (true){
            try {
                jsonObject = JSONUtil.parseObj(doPost(
                        YsQueryDtoSingletonUtil.getYsQueryDto().getGatewayUrl() + OpenApiUrlEnum.ENTERPRISEBANK_LIST
                        ,paramMap
                        ,YsQueryDtoSingletonUtil.getYsQueryDto()));

                jsonArray = jsonObject.getJSONObject("data").getJSONArray("recordList");
                //汇总银行信息到列表中
                for (int i=0;i<jsonArray.size();i++){
                    JSONObject jsonObjectBank = jsonArray.getJSONObject(i);
                    bankList.add(jsonObjectBank);
                }
                //更新分页页面获取新数据
                if (jsonObject.getJSONObject("data").getLong("endPageIndex") > 0) {
                    if (pageIndex < jsonObject.getJSONObject("data").getLong("endPageIndex")) {
                        pageIndex++;
                        paramMap.put("pageIndex",pageIndex);
                    }else {
                        break;
                    }
                }
            } catch (Exception e) {
                logger.error("错误信息{}", e.getMessage());
                return null;
            }

        }
        return bankList;
    }
    //供应商列表查询
    public List<JSONObject> getVendorDetailsMessage() {

        long pageSize = 500;
        long pageIndex = 1;
        Map<String,Object> paramMap = new HashMap() {
            {
                put("pageSize", pageSize);
                put("pageIndex", 1);
                put("vendorApplyRange_isCreator",true);
            }
        };
        JSONObject jsonObject;
        JSONArray jsonArray;
        List<JSONObject> vendorList = new ArrayList<>(); //汇总供应商信息
        while (true) {
            try {
                jsonObject = JSONUtil.parseObj(doPost(
                        YsQueryDtoSingletonUtil.getYsQueryDto().getGatewayUrl() + OpenApiUrlEnum.VENDOR_LIST
                        , paramMap
                        , YsQueryDtoSingletonUtil.getYsQueryDto()));

                jsonArray = jsonObject.getJSONObject("data").getJSONArray("recordList");
                //汇总银行信息到列表中
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObjectVendor = jsonArray.getJSONObject(i);
                    vendorList.add(jsonObjectVendor);
                }
                //更新分页页面获取新数据
                if (jsonObject.getJSONObject("data").getLong("endPageIndex") > 0) {
                    if (pageIndex < jsonObject.getJSONObject("data").getLong("endPageIndex")) {
                        pageIndex++;
                        paramMap.put("pageIndex", pageIndex);
                    } else {
                        break;
                    }
                }
            } catch (Exception e) {
                logger.error("错误信息{}", e.getMessage());
                return null;
            }

        }
        return vendorList;

    }
    //供应商详情查询(检查是否有银行账户)
    public JSONObject getVendorBankCode(String vendorId){
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("id",vendorId);
        String code = null;
        JSONObject jsonObject = null;
        try {
            jsonObject = JSONUtil.parseObj(doGet(YsQueryDtoSingletonUtil.getYsQueryDto().getGatewayUrl() + OpenApiUrlEnum.VENDOR_DETAIL, params, YsQueryDtoSingletonUtil.getYsQueryDto()));

            JSONObject jsonObject1= jsonObject.getJSONObject("data");

            if (jsonObject1 != null && !jsonObject1.isEmpty()) {

                return jsonObject1;
            }else {
                throw new Exception("ys系统中供应商档案数据错误");
            }
        } catch (Exception e) {
            //logger.error("错误处理{}", e.getMessage());
        }
        return jsonObject;
    }

    //供应商新增
    public JSONObject addVendorBanks(Map<String,Object> paramMap){
        JSONObject jsonObject = null;
        try {
            jsonObject = JSONUtil.parseObj(doPost(YsQueryDtoSingletonUtil.getYsQueryDto().getGatewayUrl() + OpenApiUrlEnum.VENDOR_SAVE, paramMap, YsQueryDtoSingletonUtil.getYsQueryDto()));
            return jsonObject;
        } catch (Exception e) {
            logger.error("供应商新增错误信息{}", e.getMessage());
        }
        return jsonObject;
    }

    //银行网点查询
    public JSONObject getBankDotList(String bankName){
        JSONObject jsonObject = null;
        long pageSize = 10;
        long pageIndex = 1;

        Map<String,Object> paramMap = new HashMap() {
            {
                put("pageSize", pageSize);
                put("pageIndex", 1);
                put("name",bankName);
            }
        };
        try {
            jsonObject = JSONUtil.parseObj(doPost(YsQueryDtoSingletonUtil.getYsQueryDto().getGatewayUrl() + OpenApiUrlEnum.BANKDOT_LIST, paramMap, YsQueryDtoSingletonUtil.getYsQueryDto()));

            return jsonObject;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}


